#include "sharefiledialog.h"
#include "ui_sharefiledialog.h"
#include<QDebug>
#include<QCheckBox>
ShareFileDialog::ShareFileDialog(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::ShareFileDialog)
{
    ui->setupUi(this);
    setFixedSize(660,440);
    setAttribute(Qt::WA_DeleteOnClose);
    setWindowModality(Qt::ApplicationModal);
    setWindowIcon(QIcon(":/img/application.png"));
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->setAlternatingRowColors(true);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);        //设置不可编辑

    ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed); // 设为固定模式
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(60);                // 每行高度 40 像素
}

ShareFileDialog::~ShareFileDialog()
{
    delete ui;
}


void ShareFileDialog::on_selectAllRadioBtn_clicked()
{
    int rowCount = ui->tableWidget->rowCount();
    for(int i = 0; i < rowCount; i++){
        QWidget *cellWidget = ui->tableWidget->cellWidget(i, 1);
        if(cellWidget){
            QCheckBox *checkBox = cellWidget->findChild<QCheckBox *>();
            if(checkBox){
                checkBox->setCheckState(Qt::Checked);
            }
        }
    }
}


void ShareFileDialog::on_cancelSelectRadioBtn_clicked()
{
    int rowCount = ui->tableWidget->rowCount();
    for(int i = 0; i < rowCount; i++){
        QWidget *cellWidget = ui->tableWidget->cellWidget(i, 1);
        if(cellWidget){
            QCheckBox *checkBox = cellWidget->findChild<QCheckBox *>();
            if(checkBox){
                checkBox->setCheckState(Qt::Unchecked);
            }
        }
    }
}


void ShareFileDialog::on_confirmBtn_clicked()
{
    QList<QString>shareList;
    int rowCount = ui->tableWidget->rowCount();
    for(int i = 0; i < rowCount; i++){
        QWidget *cellWidget = ui->tableWidget->cellWidget(i, 1);
        if(cellWidget){
            QCheckBox *checkBox = cellWidget->findChild<QCheckBox *>();
            if(checkBox){
                if(checkBox->checkState()==Qt::Checked){
                    QString coltext = ui->tableWidget->item(i, 0)->text();
                    shareList.append(coltext);
                }
            }
        }
    }
    if(shareList.size()>0){
        emit sendShareList(shareList);
    }
    close();
}


void ShareFileDialog::on_cancelBtn_clicked()
{
    close();
}

void ShareFileDialog::getFriendList(const QList<QString> &usrList)
{
    ui->tableWidget->clearContents();  // 清除单元格内容，但保留表头和结构
    ui->tableWidget->setRowCount(0);   // 设置为0行，从而移除所有行

    for(int i=0;i<usrList.size();i++){
        ui->tableWidget->insertRow(i);       // 插入新行
        QTableWidgetItem *item = new QTableWidgetItem(usrList.at(i));
        item->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget->setItem(i, 0, item);
        QCheckBox *checkBox=new QCheckBox;
        checkBox->setToolTip("选中好友");
        QWidget *widget = new QWidget;
        QHBoxLayout *layout = new QHBoxLayout(widget);
        layout->addWidget(checkBox);
        layout->setAlignment(Qt::AlignCenter);   // 居中
        layout->setContentsMargins(0, 0, 0, 0);  // 去掉边距
        ui->tableWidget->setCellWidget(i, 1, widget);
    }
}

